package org.arain.baseii.security;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.arain.baseii.exception.UseException;
import org.arain.baseii.login.LoginDto;
import org.arain.baseii.login.LoginService;
import org.arain.baseii.pojo.SysResource;
import org.arain.baseii.pojo.SysUser;
import org.arain.baseii.system.resources.SysResourcesService;
import org.arain.baseii.utils.ArainResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 
* <p>Title:SampleRealm </p>
* @author Arain
* @date2017年6月29日
 */
@Component
public class SimpleRealm extends AuthorizingRealm {
	@Autowired
	private LoginService loginService;
	@Autowired
	private SysResourcesService sysResourcesService;
	public SimpleRealm() {
		super();
	}
	/**
	 *  认证信息，主要针对用户登录， 
	 */
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken)authcToken;
		LoginDto loginDto = new LoginDto();
		loginDto.setUsername(token.getUsername());
		loginDto.setPassword(String.valueOf(token.getPassword()));
		ArainResult result = loginService.load_login(loginDto);
		if(result.getStatus() == 203) {
			throw new UseException("账号或密码错误");  
		} else if(result.getStatus() == 204) {
			throw new UseException("账号或密码错误");
		} else {
			SysUser user = (SysUser) result.getData();
			return new SimpleAuthenticationInfo(user,user.getPassword(), getName());
		}
		
    }

	 /** 
     * 授权 
     */  
    @Override  
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
    	SimpleAuthorizationInfo info =  new SimpleAuthorizationInfo();
    	SysUser user = (SysUser) principals.getPrimaryPrincipal();
    	List<SysResource> list = sysResourcesService.load_all_resources(user);
    	for (SysResource sysResource : list) {
			info.addStringPermission(String.valueOf(sysResource.getTitle()));
		}
    	System.out.println(info.getStringPermissions());
        return info;  
    }  
    /**
     * 清空当前用户权限信息
     */
	public  void clearCachedAuthorizationInfo() {
		PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals();
		SimplePrincipalCollection principals = new SimplePrincipalCollection(
				principalCollection, getName());
		super.clearCachedAuthorizationInfo(principals);
	}
	/**
	 * 指定principalCollection 清除
	 */
	public void clearCachedAuthorizationInfo(PrincipalCollection principalCollection) {
		SimplePrincipalCollection principals = new SimplePrincipalCollection(
				principalCollection, getName());
		super.clearCachedAuthorizationInfo(principals);
	}
}
